/* This file is part of BonQ - A simple HTTP-based messaging server.
 * Copyright (C) 2009 James Cook
 *
 * BonQ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License, version 2, as published by the
 * Free Software Foundation.
 *
 * BonQ is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License, version 2,
 * for more details.
 *
 * You should have received a copy of the GNU General Public License, version
 * 2, along with BonQ.  If not, see:
 *
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */
package com.bonkabonka.queue;

import java.io.IOException;

/**
 * This class defines all the commands available through the SystemQueue.
 * 
 * @author jamesc
 */
public class SystemCommandHandler
{
    private QueueBucket bucket;

    /**
     * This constructor provides the QueueBucket to act on.
     * 
     * @param bucket QueueBucket to apply all the commands to
     */
    public SystemCommandHandler(QueueBucket bucket)
    {
        this.bucket = bucket;
    }

    /**
     * Add a queue of the specified type with the specified name
     * 
     * @param type String containing the Queue type
     * @param name String containing the Queue name
     * @throws java.lang.IllegalArgumentException from QueueBucket.add
     * @throws java.lang.InstantiationException from QueueBucket.add
     * @throws java.lang.IllegalAccessException from QueueBucket.add
     */
    public void add(String type, String name)
        throws IllegalArgumentException,
               InstantiationException,
               IllegalAccessException
    {
        bucket.add(name, type);
    }

    /**
     * Remove the queue identified by the given name.
     * 
     * @param name String containing the Queue name
     * @throws java.lang.IllegalArgumentException if the queue doesn't exist
     */
    public void del(String name)
        throws IllegalArgumentException
    {
        bucket.remove(name);
    }

    /**
     * Get a directory of queue names.
     * 
     * @return String containing queue names separated by a newline
     */
    public String list()
    {
        StringBuffer list = new StringBuffer();
        
        for(String name: bucket.list())
        {
            list.append(name);
            list.append("\n");
        }
        
        return list.toString();
    }

    /**
     * Have the QueueBucket try to save itself to disk.
     * 
     * @throws java.io.IOException from QueueBucket.save
     */
    public void save()
        throws IOException
    {
        bucket.save();
    }
}
